<?php
include_once 'model/Table.php';

class cartModel {
	function __construct() {
		if (isset($_SESSION["products"])) {
			$this -> products = $_SESSION["products"];
		} else {
			$this -> products = array();
			$_SESSION["products"] = $this -> products;
		}

		$this -> message = "";
	}

	function add($id, $quantity) {
		//$quantity = $this -> products[$id] + $quantity;
		$this -> products[$id] = $quantity;
		$temp = $_SESSION['products'];
		$_SESSION["products"] = $this -> products;

		if ($temp == $_SESSION["products"]) {
			$this -> message = "<b>Add fail</b>";
		} else {
			$this -> message = "<b></b>";
		}

	}

	public function addBill($deliverDate, $deliveryAddress, $Account, $Payment, $data = array()) {

		$result = FALSE;
		if (mysql_query('BEGIN')) {

			$tbl_order = new Table("tbl_order");
			$tbl_order_detail = new Table("tbl_order_detail");
			$tbl_product = new Table("tbl_product");
			
			$orderData = array("DeliveryDate" => $deliverDate, "DeliveryAddress" => $deliveryAddress, "Account" => $Account, "Payment" => $Payment);
			$tbl_order -> insert($orderData);
			$orderID = $tbl_order -> insertID;
			$flagDetail = TRUE;
			foreach ($data as $ProductID => $Quantity) {
				
				$product = $tbl_product->select("*", "WHERE ProductID=$ProductID");
				$pQuantity = $product[0][Quantity] - $Quantity;
				$producdata = array("Quantity"=> $pQuantity);
				$tbl_product->update($producdata,"ProductID='".$ProductID."'");
				
				
				$detail = array("OrderID" => $orderID, "ProductID" => $ProductID, "Quantity" => $Quantity);
				$tbl_order_detail -> insert($detail);
				if($tbl_order_detail -> insertID == 0)
				{
					$flagDetail = FALSE;
				}
			}

			if ($orderID > 0 && $flagDetail == TRUE) {
				$result = mysql_query('COMMIT');
				$_SESSION["products"]=NULL;
				header("location: cart.php?mess=Success");
				echo "Success Bill";

			}
			
			else {
				mysql_query('ROLLBACK');
				echo "UNsuccess Bill";
				
			}

		}
	}

	function remove($id) {
		unset($this -> products[$id]);
		$_SESSION["products"] = $this -> products;
		$this -> message = "Remove success";
	}

	function update($id, $quantity) {
		$this -> products[$id]["quantity"] = $quantity;

		$_SESSION["products"] = $this -> products;
	}

	function getViewedProduct($product_id = 0) {
		$flag = FALSE;
		if ($this -> phpsession -> get('viewed_product') == '')
			$this -> phpsession -> save('viewed_product', $product_id);
		else {
			$str = $product_id;
			$viewed_array = explode(':', $this -> phpsession -> get('viewed_product'));
			foreach ($viewed_array as $a) {
				if ($str == $a) {$flag = FALSE;
					break;
				} else {$flag = TRUE;
				}
			}
		}
		if ($flag)
			$this -> phpsession -> save('viewed_product', $this -> phpsession -> get('viewed_product') . ':' . $str);

		$product_ids = explode(':', $this -> phpsession -> get('viewed_product'));
		if (count($product_ids) > 4) {
			array_shift($product_ids);
			$b = '';
			foreach ($product_ids as $id) {
				if ($b == '')
					$b .= $id;
				else
					$b .= ':' . $id;
			}
			$this -> phpsession -> save('viewed_product', $b);
		}
		return explode(':', $this -> phpsession -> get('viewed_product'));
	}

}
?>